package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pureut.storage.domain.*;
import com.pureut.storage.mapper.*;
import com.pureut.storage.service.PublicSelectSysDateChangeService;
import com.pureut.storage.utils.DocumentTypeEnum;
import com.sale.storage.api.dto.SysDateChangeDto;
import com.sale.storage.api.vo.UpdateDateChangeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 日期变更出库、入库数据查询实现层
 */
@Service
@RequiredArgsConstructor
public class PublicSelectSysDateChangeServiceImpl implements PublicSelectSysDateChangeService {

    private final PickingSaleMapper pickingSaleMapper;

    private final PickingPurchaseMapper pickingPurchaseMapper;

    private final StockOutInfoMapper stockOutInfoMapper;

    private final PickingRestMapper pickingRestMapper;

    private final PurchaseReceiptMapper purchaseReceiptMapper;

    private final SalesReturnMapper salesReturnMapper;

    private final WipCompletionMapper wipCompletionMapper;

    private final AllotManagementMapper allotManagementMapper;

    private final CheckManagementMapper checkManagementMapper;

    /**
     * 出库查询
     *
     * @param documentNumber
     * @return
     */
    @Override
    public SysDateChangeDto outSelect(String documentNumber) {
        SysDateChangeDto sysDateChangeDto = new SysDateChangeDto();
        // 销售发货
        PickingSale pickingSale = pickingSaleMapper.selectOne(new QueryWrapper<PickingSale>()
            .lambda().eq(PickingSale::getWodDocNum, documentNumber));
        if (pickingSale != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType(DocumentTypeEnum.getValue(pickingSale.getDocumentType()));
            sysDateChangeDto.setOriginalBillingTime(pickingSale.getDrawerTime());
            if (pickingSale.getReviewTime() != null) {
                sysDateChangeDto.setOriginalAuditTime(pickingSale.getReviewTime());
            }
            return sysDateChangeDto;
        }
        // 采购退货
        PickingPurchase pickingPurchase = pickingPurchaseMapper.selectOne(new QueryWrapper<PickingPurchase>()
            .lambda().eq(PickingPurchase::getWodDocNum, documentNumber));
        if (pickingPurchase != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType(DocumentTypeEnum.getValue(pickingPurchase.getDocumentType()));
            sysDateChangeDto.setOriginalBillingTime(pickingPurchase.getDrawerTime());
            if (pickingPurchase.getReviewTime() != null) {
                sysDateChangeDto.setOriginalAuditTime(pickingPurchase.getReviewTime());
            }
            return sysDateChangeDto;
        }
        // 领料出库
        StockOutInfo stockOutInfo = stockOutInfoMapper.selectOne(new QueryWrapper<StockOutInfo>()
            .lambda().eq(StockOutInfo::getWodDocNum, documentNumber));
        if (stockOutInfo != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType(DocumentTypeEnum.getValue(stockOutInfo.getDocumentType()));
            sysDateChangeDto.setOriginalBillingTime(stockOutInfo.getDrawerTime());
//            if (pickingPurchase.getReviewTime() != null) {
//                sysDateChangeDto.setOriginalAuditTime(pickingPurchase.getReviewTime());
//            }
            return sysDateChangeDto;
        }
        // 其他出库
        PickingRest pickingRest = pickingRestMapper.selectOne(new QueryWrapper<PickingRest>()
            .lambda().eq(PickingRest::getWodDocNum, documentNumber));
        if (pickingRest != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType(DocumentTypeEnum.getValue(pickingRest.getDocumentType()));
            sysDateChangeDto.setOriginalBillingTime(pickingRest.getDrawerTime());
//            if (pickingPurchase.getReviewTime() != null) {
//                sysDateChangeDto.setOriginalAuditTime(pickingPurchase.getReviewTime());
//            }
            return sysDateChangeDto;
        }
        return sysDateChangeDto;
    }


    /**
     * 入库查询
     *
     * @param documentNumber
     * @return
     */
    @Override
    public SysDateChangeDto inSelect(String documentNumber) {
        SysDateChangeDto sysDateChangeDto = new SysDateChangeDto();
        // 采购入库
        PurchaseReceipt purchaseReceipt = purchaseReceiptMapper.selectOne(new QueryWrapper<PurchaseReceipt>()
            .lambda().eq(PurchaseReceipt::getReceiptNo, documentNumber));
        if (purchaseReceipt != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType(DocumentTypeEnum.getValue(purchaseReceipt.getDocumentType()));
            sysDateChangeDto.setOriginalBillingTime(purchaseReceipt.getDrawerTime());
//            if (purchaseReceipt.getReviewTime() != null) {
//                sysDateChangeDto.setOriginalAuditTime(purchaseReceipt.getReviewTime());
//            }
            return sysDateChangeDto;
        }
        // 销售退货
        SalesReturn salesReturn = salesReturnMapper.selectOne(new QueryWrapper<SalesReturn>()
            .lambda().eq(SalesReturn::getReceiptNo, documentNumber));
        if (salesReturn != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType(DocumentTypeEnum.getValue(salesReturn.getDocumentType()));
            sysDateChangeDto.setOriginalBillingTime(salesReturn.getDrawerTime());
//            if (purchaseReceipt.getReviewTime() != null) {
//                sysDateChangeDto.setOriginalAuditTime(purchaseReceipt.getReviewTime());
//            }
            return sysDateChangeDto;
        }
        // 完工入库
        WipCompletion wipCompletion = wipCompletionMapper.selectOne(new QueryWrapper<WipCompletion>()
            .lambda().eq(WipCompletion::getReceiptNo, documentNumber));
        if (wipCompletion != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType(DocumentTypeEnum.getValue(wipCompletion.getDocumentType()));
            sysDateChangeDto.setOriginalBillingTime(wipCompletion.getDrawerTime());
//            if (purchaseReceipt.getReviewTime() != null) {
//                sysDateChangeDto.setOriginalAuditTime(purchaseReceipt.getReviewTime());
//            }
            return sysDateChangeDto;
        }
        // 其他入库
        return sysDateChangeDto;
    }

    /**
     * 库内查询
     *
     * @param documentNumber
     * @return
     */
    @Override
    public SysDateChangeDto kuneSelect(String documentNumber) {
        SysDateChangeDto sysDateChangeDto = new SysDateChangeDto();
        // 盘点单
        CheckManagement checkManagement = checkManagementMapper.selectOne(new QueryWrapper<CheckManagement>()
            .lambda().eq(CheckManagement::getCheckDoc, documentNumber));
        if (checkManagement != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType("盘点单");
            sysDateChangeDto.setOriginalBillingTime(checkManagement.getCreateTime());
            if (checkManagement.getReviewTime() != null) {
                sysDateChangeDto.setOriginalAuditTime(checkManagement.getReviewTime());
            }
            return sysDateChangeDto;
        }

        // 调拨单
        AllotManagement allotManagement = allotManagementMapper.selectOne(new QueryWrapper<AllotManagement>()
            .lambda().eq(AllotManagement::getAllotDoc, documentNumber));
        if (allotManagement != null) {
            //添加单据类型
            sysDateChangeDto.setDocumentType("调拨单");
            sysDateChangeDto.setOriginalBillingTime(allotManagement.getCreateTime());
//            if (purchaseReceipt.getReviewTime() != null) {
//                sysDateChangeDto.setOriginalAuditTime(purchaseReceipt.getReviewTime());
//            }
            return sysDateChangeDto;
        }
        return sysDateChangeDto;
    }

    /**
     * 出库修改
     *
     * @param updateDateChangeVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean outUpdate(UpdateDateChangeVo updateDateChangeVo) {
        // 销售发货
        PickingSale pickingSale = pickingSaleMapper.selectOne(new QueryWrapper<PickingSale>()
            .lambda().eq(PickingSale::getWodDocNum, updateDateChangeVo.getDocumnetNumber()));
        if (pickingSale != null) {
            //修改单据
            pickingSale.setDrawerTime(updateDateChangeVo.getChangeBillingTime());
            pickingSale.setReviewTime(updateDateChangeVo.getChangeAuditTime());
            return pickingSaleMapper.updateById(pickingSale) > 0;
        }
        // 采购退货
        PickingPurchase pickingPurchase = pickingPurchaseMapper.selectOne(new QueryWrapper<PickingPurchase>()
            .lambda().eq(PickingPurchase::getWodDocNum, updateDateChangeVo.getDocumnetNumber()));
        if (pickingPurchase != null) {
            //修改单据
            pickingPurchase.setDrawerTime(updateDateChangeVo.getChangeBillingTime());
            pickingPurchase.setReviewTime(updateDateChangeVo.getChangeAuditTime());
            return pickingPurchaseMapper.updateById(pickingPurchase) > 0;
        }
        // 领料出库
        StockOutInfo stockOutInfo = stockOutInfoMapper.selectOne(new QueryWrapper<StockOutInfo>()
            .lambda().eq(StockOutInfo::getWodDocNum, updateDateChangeVo.getDocumnetNumber()));
        if (stockOutInfo != null) {
            //修改单据
            stockOutInfo.setDrawerTime(updateDateChangeVo.getChangeBillingTime());
            return stockOutInfoMapper.updateById(stockOutInfo) > 0;
        }
        // 其他出库
        PickingRest pickingRest = pickingRestMapper.selectOne(new QueryWrapper<PickingRest>()
            .lambda().eq(PickingRest::getWodDocNum, updateDateChangeVo.getDocumnetNumber()));
        if (pickingRest != null) {
            //修改单据
            pickingRest.setDrawerTime(updateDateChangeVo.getChangeBillingTime());
            return pickingRestMapper.updateById(pickingRest) > 0;
        }
        return false;
    }

    /**
     * 入库修改
     *
     * @param updateDateChangeVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean inUpdate(UpdateDateChangeVo updateDateChangeVo) {
        // 采购入库
        PurchaseReceipt purchaseReceipt = purchaseReceiptMapper.selectOne(new QueryWrapper<PurchaseReceipt>()
            .lambda().eq(PurchaseReceipt::getReceiptNo, updateDateChangeVo.getDocumnetNumber()));
        if (purchaseReceipt != null) {
            purchaseReceipt.setDrawerTime(updateDateChangeVo.getChangeBillingTime());
            return purchaseReceiptMapper.updateById(purchaseReceipt) > 0;
        }
        // 销售退货
        SalesReturn salesReturn = salesReturnMapper.selectOne(new QueryWrapper<SalesReturn>()
            .lambda().eq(SalesReturn::getReceiptNo, updateDateChangeVo.getDocumnetNumber()));
        if (salesReturn != null) {
            salesReturn.setDrawerTime(updateDateChangeVo.getChangeBillingTime());
            salesReturn.setReviewTime(updateDateChangeVo.getChangeAuditTime());
            return salesReturnMapper.updateById(salesReturn) > 0;
        }
        // 完工入库
        WipCompletion wipCompletion = wipCompletionMapper.selectOne(new QueryWrapper<WipCompletion>()
            .lambda().eq(WipCompletion::getReceiptNo, updateDateChangeVo.getDocumnetNumber()));
        if (wipCompletion != null) {
            wipCompletion.setDrawerTime(updateDateChangeVo.getChangeBillingTime());
            return wipCompletionMapper.updateById(wipCompletion) > 0;
        }
        // 其他入库
        return false;
    }

    /**
     * 库内修改
     *
     * @param updateDateChangeVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean kuneUpdate(UpdateDateChangeVo updateDateChangeVo) {
        SysDateChangeDto sysDateChangeDto = new SysDateChangeDto();
        // 盘点单
        CheckManagement checkManagement = checkManagementMapper.selectOne(new QueryWrapper<CheckManagement>()
            .lambda().eq(CheckManagement::getCheckDoc, updateDateChangeVo.getDocumnetNumber()));
        if (checkManagement != null) {
            //修改单据
            checkManagement.setCreateTime(updateDateChangeVo.getChangeBillingTime());
            checkManagement.setReviewTime(updateDateChangeVo.getChangeAuditTime());
            return checkManagementMapper.updateById(checkManagement) > 0;
        }

        // 调拨单
        AllotManagement allotManagement = allotManagementMapper.selectOne(new QueryWrapper<AllotManagement>()
            .lambda().eq(AllotManagement::getAllotDoc, updateDateChangeVo.getDocumnetNumber()));
        if (allotManagement != null) {
            //添加单据类型
            allotManagement.setCreateTime(updateDateChangeVo.getChangeBillingTime());
            return allotManagementMapper.updateById(allotManagement) > 0;
        }
        return false;
    }
}
